﻿package mortal.game.view.market.sale
{
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.events.*;
    import com.mui.utils.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;

    public class MktBag extends GSprite
    {
        private var _bg:ScaleBitmap;
        private var _bgTitle:ScaleBitmap;
        private var _title:GBitmap;
        private var _list:GTileList;
        private var _pageTabBar:GTabBar;
        private var _bagPageData:Array;
        private var _spMoneys:MktMoneys;
        private var _selectSp:ScaleBitmap;
        private var _selectItemData:ItemData;

        public function MktBag()
        {
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this._bagPageData = [{label:"1", name:"page1"}, {label:"2", name:"page2"}, {label:"3", name:"page3"}, {label:"4", name:"page4"}];
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.bg(0, 0, 328, 345, this);
            this._bgTitle = UIFactory.bg(0, 0, 323, 25, this, "RegionTitleBg");
            this._title = UIFactory.bitmap(ImagesConst.market1, 0, 0, this);
            this._title.x = (this._bgTitle.width - this._title.width) / 2;
            this._title.y = (this._bgTitle.y + this._bgTitle.height - this._title.height) / 2;
            this._list = UIFactory.tileList(4, 30, 330, 307, this);
            this._list.rowHeight = 44;
            this._list.columnWidth = 44;
            this._list.horizontalGap = 0;
            this._list.verticalGap = 0;
            this._list.setStyle("cellRenderer", MktBagItemRenderer);
            this._selectSp = UIFactory.bg(0, 0, 45, 45, this._list, ImagesConst.selectFilter);
            this._selectSp.visible = false;
            this._pageTabBar = UIFactory.gTabBar(2, 333, this._bagPageData, 20, 20, this, this.pageChangeHandler, "PageBtn");
            this._pageTabBar.selectedIndex = 0;
            this._pageTabBar.drawNow();
            this._pageTabBar.visible = false;
            this._spMoneys = UICompomentPool.getUICompoment(MktMoneys);
            this._spMoneys.createDisposedChildren();
            this._spMoneys.x = 5;
            this._spMoneys.y = 355;
            addChild(this._spMoneys);
            Dispatcher.addEventListener(EventName.MarketRemoveSaleItem, this.onRemoveSeekItem);
            this._list.configEventListener(Event.CHANGE, this.listSelectChange);
            this.addEventListener(Event.ADDED_TO_STAGE, this.onAddToStage);
            return;
        }// end function

        private function onRemoveSeekItem(event:DataEvent) : void
        {
            this._selectItemData = null;
            if (this._list)
            {
                this._list.selectedIndex = -1;
                this.listSelectChange(null);
            }
            return;
        }// end function

        private function listSelectChange(event:Event) : void
        {
            var _loc_3:MktBagItemRenderer = null;
            if (this._list.selectedIndex == -1)
            {
                this.setSelectSpPos(null);
                return;
            }
            var _loc_2:* = this._list.selectedItem;
            if (_loc_2 is ItemData)
            {
                _loc_3 = this._list.itemToCellRenderer(_loc_2) as MktBagItemRenderer;
                this._selectItemData = _loc_2 as ItemData;
                Dispatcher.dispatchEvent(new DataEvent(EventName.MarketPushSaleItem, _loc_2));
                this.setSelectSpPos(_loc_3);
            }
            return;
        }// end function

        private function setSelectSpPos(param1:MktBagItemRenderer) : void
        {
            return;
        }// end function

        protected function onAddToStage(event:Event) : void
        {
            this.removeEventListener(Event.ADDED_TO_STAGE, this.onAddToStage);
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.onRemoveFormStage);
            this.updateMktBag();
            NetDispatcher.addCmdListener(ServerCommand.BackpackAllDataGet, this.updateMktBag);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.updateMktBag);
            return;
        }// end function

        protected function onRemoveFormStage(event:Event) : void
        {
            this.removeEventListener(Event.REMOVED_FROM_STAGE, this.onRemoveFormStage);
            NetDispatcher.removeCmdListener(ServerCommand.BackpackAllDataGet, this.updateMktBag);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.updateMktBag);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._selectItemData = null;
            this._bg.dispose(param1);
            this._bgTitle.dispose(param1);
            this._title.dispose(param1);
            this._selectSp.dispose(param1);
            this._list.dispose(param1);
            this._pageTabBar.dispose(param1);
            this._spMoneys.dispose(param1);
            this._bg = null;
            this._bgTitle = null;
            this._title = null;
            this._selectSp = null;
            this._list = null;
            this._pageTabBar = null;
            this._spMoneys = null;
            return;
        }// end function

        private function pageChangeHandler(event:MuiEvent) : void
        {
            var _loc_2:ItemData = null;
            var _loc_3:MktBagItemRenderer = null;
            var _loc_4:int = 0;
            this.updateMktBag();
            this._list.selectedIndex = -1;
            if (this._selectItemData)
            {
                _loc_4 = 0;
                while (_loc_4 < this._list.dataProvider.length)
                {
                    
                    _loc_2 = this._list.getItemAt(_loc_4) as ItemData;
                    if (_loc_2 && _loc_2.itemCode == this._selectItemData.itemCode)
                    {
                        this._list.selectedIndex = _loc_4;
                        break;
                    }
                    _loc_4++;
                }
            }
            _loc_3 = this._list.itemToCellRenderer(_loc_2) as MktBagItemRenderer;
            this.setSelectSpPos(_loc_3);
            return;
        }// end function

        public function updateMktBag(param1:Object = null) : void
        {
            this._list.dataProvider = this.getDataProvider();
            this._list.drawNow();
            return;
        }// end function

        private function getDataProvider() : DataProvider
        {
            var _loc_2:Array = null;
            var _loc_1:* = new DataProvider();
            var _loc_3:int = 49;
            var _loc_4:* = this._pageTabBar.selectedIndex * _loc_3;
            var _loc_5:* = _loc_4 + _loc_3;
            if (Cache.instance.pack.backPackCache && Cache.instance.pack.backPackCache.sbag)
            {
                _loc_2 = Cache.instance.pack.backPackCache.getAllItemsCanSaleMarket();
            }
            var _loc_6:* = _loc_4;
            while (_loc_6 < _loc_5)
            {
                
                if (_loc_2 && _loc_2[_loc_6])
                {
                    _loc_1.addItem(_loc_2[_loc_6]);
                }
                else
                {
                    _loc_1.addItem(new Object());
                }
                _loc_6++;
            }
            return _loc_1;
        }// end function

    }
}
